package com.qingxin.statistics.action;

import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.utils.ExcelUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 统计报表
 * @author Administrator
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsAction extends BaseAction {

	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private FarmParkService farmParkService;
	
	
	/**
	 * 种养单位汇总表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/zydwhz_list.do", method=RequestMethod.GET)
	public String zydwhz_list(ModelMap model, HttpServletRequest request){
		return "trace/statistics/zydwhz_list";
	}
	
	
	/**
	 * 种养单位汇总表数据
	 * @param datatype
	 * @param leftdata
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/zydwhz_listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object zydwhz_listdata(String datatype, String leftdata, PageParam pageParam, HttpServletRequest request){
		String groupid = "";
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			groupid = leftdata;
		}
		
		return companyService.list(groupid, null, null, null, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 种养单位汇总表数据导出
	 * @param model
	 * @param datatype
	 * @param leftdata
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/zydwhz_export.do", method=RequestMethod.GET)
	public String zydwhz_export(ModelMap model, String datatype, String leftdata,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String groupid = "";
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			groupid = leftdata;
		}
		
		String temps = servletContext.getRealPath("/WEB-INF/excelTemps/zydwhz_export.xlsx");
		File tempsFile = new File(temps);
		if(!tempsFile.isFile()){
			responseText("数据模板文件丢失，导出失败。", response);
			return null;
		}
		
		List<Company> list = companyService.listCompanies(groupid, getLoginUser(request));
		if(list == null){
			list = new ArrayList<Company>();
		}
		
		String filename = "种养单位汇总数据.xlsx";
		filename = java.net.URLEncoder.encode(filename, "UTF-8");
		
		response.resetBuffer();
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename*=utf-8'zh_cn'" + filename);
		
		OutputStream outputStream = response.getOutputStream();
		ExcelUtil.exportExcel(tempsFile, outputStream, list);
		outputStream.close();
		return null;
	}
	

	
	/**
	 * 生产主体统计表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/sczttj_list.do", method=RequestMethod.GET)
	public String sczttj_list(ModelMap model, HttpServletRequest request){
		return "trace/statistics/sczttj_list";
	}
	
	
	/**
	 * 生产主体统计表数据
	 * @param datatype
	 * @param leftdata
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/sczttj_list.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object sczttj_list(String datatype, String leftdata, PageParam pageParam, HttpServletRequest request){
		String groupid = "";
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			groupid = leftdata;
		}
		return farmParkService.list(groupid, null, null, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 生产主体统计表数据导出
	 * @param model
	 * @param datatype
	 * @param leftdata
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/sczttj_export.do", method=RequestMethod.GET)
	public String sczttj_export(ModelMap model, String datatype, String leftdata,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String groupid = "";
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			groupid = leftdata;
		}
		
		String temps = servletContext.getRealPath("/WEB-INF/excelTemps/sczttj_export.xlsx");
		File tempsFile = new File(temps);
		if(!tempsFile.isFile()){
			responseText("数据模板文件丢失，导出失败。", response);
			return null;
		}
		
		List<FarmPark> list = farmParkService.listFarmParks(groupid, getLoginUser(request));
		if(list == null){
			list = new ArrayList<FarmPark>();
		}
		
		String filename = "生产主体统计数据.xlsx";
		filename = java.net.URLEncoder.encode(filename, "UTF-8");
		
		response.resetBuffer();
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename*=utf-8'zh_cn'" + filename);
		
		OutputStream outputStream = response.getOutputStream();
		ExcelUtil.exportExcel(tempsFile, outputStream, list);
		outputStream.close();
		return null;
	}
	
	
	
	
	
	//向响应写入文本
	protected void responseText(String text, HttpServletResponse response) throws Exception {
		response.resetBuffer();
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write("<html><body>");
		out.write("<h2>"+text+"</h2>");
		out.write("</body></html>");
		out.close();
	}
	
}
